<?php

namespace console\bootstrap;

use \console\core\Router,\console\Config,\console\http\Request,\console\http\Response;

class Rest
{
    static function run()
    {

        try
        {
            //实例化所需类
            $_appManager        = new \Phalcon\Mvc\Micro();
            $_controllerManager = new \Phalcon\Mvc\Micro\Collection();

            //解析参数
            $_routerParam = Request::get('_url');

            //路由匹配
            Router::batchAdd(Config::loadItem('router',true));
            Router::handle($_routerParam);
            
            //路由验证
            if(Router::wasMatched())
            {
                //解析命名空间
                $_getControllerName = ucfirst(Router::getControllerName()).'Controller';
                if(empty($_getNamespace = Router::getNamespaceName()))
                {
                    $_parseNamespaceArray = [ITEM_NAME,Config::loadItem('config')->controllerName];
                    if(Config::loadItem('config')->enableVersion)
                        array_push($_parseNamespaceArray,Request::get('_version'));
                    $_getNamespace = implode('\\',$_parseNamespaceArray);
                }

                //组装实例化的类
                $_getClassName  = "{$_getNamespace}\\{$_getControllerName}";

                //得到方法名
                $_getActionName = Router::getActionName().'Action';

                //检测类是否存在
                if(!class_exists($_getClassName))
                    return Response::json(103);

                //得到实例化后的对象
                $_objectInstance = new $_getClassName();

                //检测类方法是否存在
                if(!method_exists($_objectInstance,$_getActionName))
                    return Response::json(104);

                //绑定实例化的对象
                $_controllerManager->setHandler($_objectInstance);

                //匹配路由方法
                $_controllerManager->get($_routerParam,$_getActionName);

                //挂载到应用
                $_appManager->mount($_controllerManager);
            }
            else
                //路由检测不通过
                return Response::json(105);

            //未找到
            $_appManager->notFound(function(){
                return Response::json(404);
            });

            //事件初始化
            $_eventsManager = (new \console\core\Events())->handle();
            
            //注入初始化
            $_diManager = (new\console\core\Di())->handle();

            //事件绑定
            $_appManager->setEventsManager($_eventsManager);

            //注入绑定
            $_appManager->setDI($_diManager);

            //返回结果
            return $_appManager->handle();
        }
        catch (\Exception $e)
        {
            //检测是否开启Debug
            if(Config::loadItem('config')->debug === true)
            {
                $msg = "{$e->getFile()} LINE:{$e->getLine()} [MSG:{$e->getMessage()}]";
                $res = $e->getTrace();
            }
            else
            {
                $msg = "{$e->getMessage()} [{$e->getLine()}]";
                $res = [];
            }

            //返回错误信息
            return Response::json(100,"System Error - $msg",$res);
        }
    }
}